//
//  IncomeViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/13.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "IncomeViewController.h"
#import "HttpCustomerAction.h"
#import "IncomeInfo.h"

@interface IncomeViewController ()
@property (nonatomic,strong) NSArray *listArray;

@end

@implementation IncomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"收益";
//    self.tableView.hea
    self.tableView.separatorStyle = 0;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    UserInfo *usr = [Utils getUserInfo];
    [dic setObject:usr.id forKey:@"userId"];
    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    [HttpCustomerAction postProfitMonthly:dic block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            NSDictionary *mutableDic = result;
            NSDictionary *dic = mutableDic[@"account"];
            self.tableView.tableHeaderView = [self getHeaderView:[NSString stringWithFormat:@"%.2f",[dic[@"balance"] floatValue]]];
            self.listArray = mutableDic[@"monthProfitList"];
            [self.tableView reloadData];
        }
    }];
    
}

-(UIView*)getHeaderView:(NSString*)title{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    UIImageView *backView = [UIImageView new];
    backView.backgroundColor = [UIColor whiteColor];
    [view addSubview:backView];
    backView.sd_layout.leftSpaceToView(view, 0).topSpaceToView(view, 0).widthIs(SCREEN_WIDTH).heightIs(60);
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = [NSString stringWithFormat:@"总收益:%@元",title];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(view, 15).topSpaceToView(view, 0).widthIs(SCREEN_WIDTH-15).heightIs(60);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, SCREEN_WIDTH-20, 40)];
    label.font = [UIFont systemFontOfSize:14];
    label.text = @"月收益";
    [view addSubview:label];
    
    return view;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = 0;
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = 10;
        titleLabel.font = [UIFont systemFontOfSize:14];
        [cell.contentView addSubview:titleLabel];
        titleLabel.sd_layout.leftSpaceToView(cell.contentView, 20).topSpaceToView(cell.contentView, 0).heightIs(44).autoWidthRatio(0);
        [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
        
        UILabel *moneyLabel = [UILabel new];
        moneyLabel.tag = 11;
        moneyLabel.font = [UIFont systemFontOfSize:14];
        moneyLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:moneyLabel];
        moneyLabel.sd_layout.rightSpaceToView(cell.contentView, 20).topSpaceToView(cell.contentView, 0).heightIs(44).autoWidthRatio(0);
        [moneyLabel setSingleLineAutoResizeWithMaxWidth:200];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = DefaultCellLineColor;
        [cell.contentView addSubview:lineView];
        lineView.sd_layout.leftSpaceToView(cell.contentView, 15).bottomSpaceToView(cell.contentView, 0).rightSpaceToView(cell.contentView, 0).heightIs(1);
        
    }
    // Configure the cell...
    IncomeInfo *incomeInfo = self.listArray[indexPath.row];
    UILabel *titleLabel = [cell.contentView viewWithTag:10];
    titleLabel.text = incomeInfo.month;
    
    UILabel *moneyLabel = [cell.contentView viewWithTag:11];
    moneyLabel.text = [NSString stringWithFormat:@"%@元",incomeInfo.profit];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
